import pandas as pd
import pymysql
from com.atzl.common.States import States
from com.atzl.functions.accounting.func.Logs import CheckLogs
from com.atzl.functions.accounting.sql.QueryData import QueryDataByBase

# 模拟运算模块
class SimulationOperation(CheckLogs):  # 继承:ToDataBase

    # 模拟运算,运算中转费结果
    #   参数:
    #       id:客户工号
    #       states:省份
    #       kg:结算重量
    def simulation_operation_run(self, id, states, kg):
        conn = self.creat_conn()
        cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
        try:
            state_s = self.text_conversion(states.strip().replace(' ', ''))
            sql = QueryDataByBase.simulate_operation(id, state_s, kg)
            cur.execute(sql)
            data = cur.fetchall()
            df = pd.DataFrame(data)
            head_price = df.iloc[0, 0]
            unit_price = df.iloc[0, 1]
            cte_price = df.iloc[0, 2]
            cur.close()
            conn.close()
            return [head_price, unit_price, cte_price]
        except Exception:
            cur.close()
            conn.close()
            return False


    # 转换查询框
    def text_conversion(self, states):
        if states == '广东' or states == '广东省':
            states = States.GUANGDONG_PROVINCE
        elif states == '上海' or states == '上海市' or states == '上海省':
            states = States.SHANGHAI
        elif states == '浙江' or states == '浙江省':
            states = States.ZHEJIANG_PROVINCE
        elif states == '江苏' or states == '江苏省':
            states = States.JIANGSU_PROVINCE
        elif states == '福建' or states == '福建省':
            states = States.FUJIAN_PROVINCE
        elif states == '江西' or states == '江西省':
            states = States.JIANGXI_PROVINCE
        elif states == '广西' or states == '广西壮族自治区' or states == '广西省':
            states = States.GUANGXIZHUANG_AUTONOMOUS_REGION
        elif states == '湖南' or states == '湖南省':
            states = States.HUNAN_PROVINCE
        elif states == '湖北' or states == '湖北省':
            states = States.HUBEI_PROVINCE
        elif states == '安徽' or states == '安徽省':
            states = States.ANHUI_PROVINCE
        elif states == '海南' or states == '海南省':
            states = States.HAINAN_PROVINCE
        elif states == '山东' or states == '山东省':
            states = States.SHANDONG_PROVINCE
        elif states == '河南' or states == '河南省':
            states = States.HENAN_PROVINCE
        elif states == '山西' or states == '山西省':
            states = States.SHANX_PROVINCE
        elif states == '四川' or states == '四川省':
            states = States.SICHUAN_PROVINCE
        elif states == '陕西' or states == '陕西省':
            states = States.SHANXI_PROVINCE
        elif states == '河北' or states == '河北省':
            states = States.HEBEI_PROVINCE
        elif states == '云南' or states == '云南省':
            states = States.YUNNAN_PROVINCE
        elif states == '贵州' or states == '贵州省':
            states = States.GUIZHOU_PROVINCE
        elif states == '辽宁' or states == '辽宁省':
            states = States.LIAONING_PROVINCE
        elif states == '吉林' or states == '吉林省':
            states = States.JILIN_PROVINCE
        elif states == '黑龙江' or states == '黑龙江省':
            states = States.HEILONGJIANG_PROVINCE
        elif states == '内蒙古' or states == '内蒙古省' or states == '内蒙古自治区':
            states = States.THE_NEIMONGGOL_AUTONOMOUS_REGION
        elif states == '甘肃' or states == '甘肃省':
            states = States.GANSU_PROVINCE
        elif states == '宁夏' or states == '宁夏省' or states == '宁夏回族自治区':
            states = States.THE_NINGXIA_AUTONOMOUS_REGION
        elif states == '青海' or states == '青海省':
            states = States.QINGHAI_PROVINCE
        elif states == '新疆' or states == '新疆省' \
                or states == '新疆自治区' or states == '新疆维吾尔自治区':
            states = States.THE_XINJIANG_UYGUR_AUTONOMOUS_REGION
        elif states == '西藏' or states == '西藏省' or states == '西藏自治区':
            states = States.TIBE_AUTONOMOUS_REGION
        elif states == '北京' or states == '北京省' or states == '北京市':
            states = States.BEIJING
        elif states == '重庆' or states == '重庆省' or states == '重庆市':
            states = States.CHONGQING
        elif states == '天津' or states == '天津省' or states == '天津市':
            states = States.TIANJIN
        return states